import { ElMessage, ElLoading, ElMessageBox } from 'element-plus'

/**
 * 工具方法
 */
const utils = {
  loading: null,
  saveData(key, value) {
    localStorage.setItem(key, value)
  },
  removeData(key) {
    localStorage.removeItem(key)
  },
  getData(key) {
    return localStorage.getItem(key)
  },
  showLoadding(msg) {
    utils.loading = ElLoading.service({
      lock: true,
			text: msg ? msg : '加载中……'
    })
  },
  hideLoadding() {
    utils.loading && utils.loading.close()
  },
  closeAllMsg() {
    ElMessage.closeAll()
  },
  showError: function(msg) {
    ElMessage({
      message: msg,
      grouping: true,
      type: 'error'
    })
  },
  showSuccess: function(msg) {
    ElMessage({
      message: msg,
      grouping: true,
      type: 'success'
    })
  },
  showWarning(msg) {
    ElMessage({
      message: msg,
      grouping: true,
      type: 'warning'
    })
  },
  showMsg(msg) {
    ElMessage({
      message: msg,
      grouping: true
    })
  },
  confirm(msg, callback) {
    ElMessageBox.confirm(msg, '警告', {
      type: 'warning',
      center: true
    })
      .then(() => {
        callback && callback('ok')
      })
      .catch(() => {
        callback && callback('cancel')
      })
  },
  check: {
    checkPhone(phone) {
      const phonereg = /^1[3-9][0-9]{9}$/
      return phonereg.test(phone)
    }
  }
}

export default utils
